/**
 * Created by bai on 2015/7/11.
 */
var Reserve = function(){
    var LHUOZHUO={
        orderId:null,
        tableId:null
    }
    /*
     * 预定列表，跳转页面showPage($('#suborderMainPage'))
     * */
    var reserveListFun = function(fun){

        var options = {};
        options.successFunction = function (data) {
            $('.reserveNotList').html('');
            fun();
            $.each(data.data,function(k,v){
                var time=new Date(v.time.sec*1000);
                time=time.getHours()+":"+time.getMinutes();
                $('<dl data-function="notAcceptWeinXinReserve" data-id="'+v.id+'">\
                    <div class="listImg" data-function="notAcceptWeinXinReserve"></div>\
                        <dt data-function="notAcceptWeinXinReserve">微信预定</dt>\
                        <dd data-function="notAcceptWeinXinReserve">用餐姓名：'+v.reserve.name+'&nbsp;用餐时间：'+v.reserve.ResTime+'</dd>\
                        <div class="time" data-function="notAcceptWeinXinReserve">'+time+'</div>\
                        <div class="contDot" data-function="notAcceptWeinXinReserve"></div>\
                    </dl>').appendTo('.reserveNotList');


            })
            var optionsA = {};
            optionsA.successFunction = function (data) {
                $('.reserveYesList').html('');
                $.each(data.data,function(k,v){
                    var time=new Date(v.time.sec*1000);
                    time=time.getHours()+":"+time.getMinutes();
                    $('<dl data-function="acceptWeinXinReserve" data-id="'+v.id+'">\
                    <div class="listImg" data-function="acceptWeinXinReserve"></div>\
                        <dt data-function="acceptWeinXinReserve">预定</dt>\
                        <dd data-function="acceptWeinXinReserve">用餐姓名：'+v.reserve.name+'&nbsp;用餐时间：'+v.reserve.ResTime+'</dd>\
                        <div class="time" data-function="acceptWeinXinReserve">'+time+'</div>\
                        <div class="contDot" data-function="acceptWeinXinReserve" style="display: none"></div>\
                    </dl>').prependTo('.reserveYesList');


                })
            };
            OrderingSystem.getReserveAcceptedOrderList(optionsA);
        };
        OrderingSystem.getReserveAwaitOrderList(options);
    }
    var getDate=function(date){

        var dataOfPort=date;
        var timeNew=new Date();
        var timeNewArray=[]
        var timeArra=['今日','明天','后天'];
        var timeArray=[];
        timeArray[0]=timeNew.getFullYear();
        timeArray[1]=(timeNew.getMonth()+1);
        timeArray[2]=timeNew.getDate()
        timeNew=timeArray.join("");
        var timeS=dataOfPort.split('-').join("");

        if(timeArra[(timeS-timeNew)]){
            return timeArra[(timeS-timeNew)];
        }else{
            return "过期"
        }

    }
    /*
     * 点击预定列表里未接受的预定
     * */
    var clickReserveNotAcceptedONReserveList = function(obj,fun){
        var oId="";
        if(obj.parents('dl').data('id')){
            oId = obj.closest('dl').data('id');
        }else{
            oId=obj.data('id');
        }


        OrderingSystem.getOrderById(oId, function (data) {
            var dataOfPort=data.data[0];
            var cpList="";
            var request="";
            var time= getDate(data.data[0].reserve.ResDate)
            if(dataOfPort.request.length==0){

            }else{
                request= dataOfPort.request.specialRequest;
            }
            $.each(data.data[0].itemList,function(k,v){
                cpList+=v.name+v.count+"份、";
            })

            /*         var timeNew=new Date();
             timeNew=timeNew.getFullYear()+(timeNew.getMonth()+1)+timeNew.getDay();
             var timeS=dataOfPort.reserve.ResDate.split('-').join("");*/
            var options = {};
            options.tableClassId = dataOfPort.reserve.tableClassId;
            options.successFunction = function (dataT) {
                fun();
                $('#mainPageWechatOrderPage').html('');
                $('<div class="cont"><div class="main">\
                <dl class="orderTitle">\
                <div class="listImg"></div>\
                <dt>微信预定</dt>\
                </dl>\
                    <dl class="personInfo">\
                        <div class="listHead">联系方式</div>\
                <pre>姓名：'+dataOfPort.reserve.name+'\
                <br>\
电话：'+dataOfPort.reserve.ResMobile+'\
                </pre>\
                        <a href="tel:'+dataOfPort.reserve.ResMobile+'"></a>\
                    </dl>\
                    <dl class="severCont">\
                        <div>服务内容</div>\
                <pre>预定餐位：'+dataOfPort.reserve.tableClassName+'\
                <br>\
用餐时间：'+time+dataOfPort.reserve.ResTime+'\
                  <br>\
用餐人数：'+dataT.data[0].cap+'人\
                </pre>\
                    </dl>\
                    <dl class="billInfo">\
                        <div class="menuInfo">账单情况</div>\
                        <p class="menuDetail">菜品：'+cpList+'</p>\
                        <p class="demand">特殊需求：<span>'+request+'</span></p>\
                        <div class="go" data-function="btn_goToTakeOutOrderInfo" data-id="'+dataOfPort.id+'">〉</div>\
                    </dl>\
                    <dl class="menuChose">\
                        <div>支付状态</div>\
                        <p>未支付</p>\
                    </dl>\
                    <dl class="menuChose">\
                        <div>接受状态</div>\
                        <p>服务未接受</p>\
                    </dl>\
                    <dl class="menuChose">\
                        <div>服务状态</div>\
                        <p>服务未确认</p>\
                    </dl>\
                    <dl class="menuChose" style="display: none">\
                        <div>座位号：暂无</div>\
                        <h6>〉</h6>\
                    </dl>\
                </div></div>').appendTo('#mainPageWechatOrderPage');
                $('    <div class="bottom">\
                    <li>\
                    <div class="refuse" data-function="btn_reserveRefuseTask" data-id="'+dataOfPort.id+'">拒绝任务</div>\
                    </li>\
                    <li>\
                        <div class="accept" data-function="btn_reserveAcceptTask" data-id="'+dataOfPort.id+'">接受任务</div>\
                    </li>\
                    </div>').appendTo('#mainPageWechatOrderPage');
            };
            OrderingSystem.getTableClassByTableClassId(options);


        });
    }
    /*
     * 接受预定详情
     * */
    var acceptedReserveInfo = function(obj,fun){
        if(obj.parents('dl').data('id')){
            var oId = obj.closest('dl').data('id');
        }else{
            oId=obj.data('id');
        }
        OrderingSystem.getOrderById(oId, function (data) {
            var dataOfPort=data.data[0];
            var cpList="";
            var time= getDate(data.data[0].reserve.ResDate)
            var request='';
            var token="";
            var tokenId=[];
            var isNotPay = '';
            if(dataOfPort.tokenList[0]){
                token=dataOfPort.tokenList[0].name;
            }else{
                token='无'
            }
            $.each(dataOfPort.tokenList,function(k,v){
                tokenId.push(v.id);
            })
            if(dataOfPort.request.length==0){

            }else{
                request= dataOfPort.request.specialRequest;
            }
            isNotPay=(dataOfPort.status=="billed")?"已支付":"未支付"
            //alert(dataOfPort.targetLabel)
            //var tableLuoZhuo=(dataOfPort.targetLabel==(""||null))?"暂无":dataOfPort.targetLabel
            var tableLuoZhuo=(dataOfPort.targetLabel)?dataOfPort.targetLabel:"暂无"

            /*         var timeNew=new Date();
             timeNew=timeNew.getFullYear()+(timeNew.getMonth()+1)+timeNew.getDay();
             var timeS=dataOfPort.reserve.ResDate.split('-').join("");*/
            var options = {};
            options.tableClassId = dataOfPort.reserve.tableClassId;
            options.successFunction = function (dataT) {
                OrderingSystem.caluAmount(oId, tokenId, function (dataCalu){
                    var discountMoney=dataCalu.data[0].caluAmount?(dataCalu.data[0].caluAmount/100):"0";
                    $('#mainPagemenuDetailPage').html('');
                    fun()

                    $('<div class="cont">\
                     <div class="main">\
                    <dl class="orderTitle">\
                        <div class="listImg"></div>\
                        <dt>预定</dt>\
                    </dl>\
                    <dl class="personInfo">\
                    <div class="listHead">联系方式</div>\
                    <pre>姓名：'+dataOfPort.reserve.name+'<br>\
电话：'+dataOfPort.reserve.ResMobile+'\
                </pre>\
                <a href="tel:'+dataOfPort.reserve.ResMobile+'"></a>\
                </dl>\
                <dl class="severCont">\
                    <div>服务内容</div>\
                <pre>预定餐位：'+dataOfPort.reserve.tableClassName+'<br>\
用餐时间：'+time+dataOfPort.reserve.ResTime+'<br>\
用餐人数：'+dataT.data[0].cap+'人\
                </pre>\
                </dl>\
                <dl class="menuChose">\
                    <div>支付状态</div>\
                    <p>'+isNotPay+'</p>\
                    </dl>\
                        <dl class="menuChose">\
                            <div>接受状态</div>\
                            <p>'+dataOfPort.userName+'接受任务</p>\
                        </dl>\
                        <dl class="menuChose">\
                            <div>座位号：'+tableLuoZhuo+'</div>\
                        </dl>\
                        <dl class="menuChose">\
                            <div>订单详情：</div>\
                        </dl>\
    <dl class="menuInfo">\
                     <div class="listHead">\
                    <pre>商品数量：'+dataOfPort.itemList.length+'<br /><br />特殊需求：<span>'+request+'</span>\
                    </pre>\
                    </div>\
                    <pre class="menuSum">共计：'+dataOfPort.total/100+'<br />优惠券：'+token+'<br />合计：'+discountMoney+'<br />\
                </pre>\
                    </dl>\
                       <div class="acceptedReserveInfoCpList">\
                        </div>\
                    </div>\
                    </div>').appendTo('#mainPagemenuDetailPage');

                    $.each(dataOfPort.itemList,function(k,v){
                        $('<dl class="menuList">\
                    <p class="dishesName">'+v.name+'</p>\
                    <p class="price">￥'+v.price/100+'</p>\
                    <p class="count">*'+v.count+'</p>\
                    </dl>').appendTo('.acceptedReserveInfoCpList');
                    })
                });


            };
            OrderingSystem.getTableClassByTableClassId(options);


        });
    }
    /*
     *点击拒绝接受预定
     * */
    var refuseReserve = function(obj,fun){
        var oId=obj.data('id');
        OrderingSystem.getOrderById(oId, function (data) {
            var dataOfPort=data.data[0];
            var cpList="";
            var time= getDate(data.data[0].reserve.ResDate)
            if(dataOfPort.status=="pre"){
                $.each(data.data[0].itemList,function(k,v){
                    cpList+=v.name+v.count+"份、";
                })
                /*         var timeNew=new Date();
                 timeNew=timeNew.getFullYear()+(timeNew.getMonth()+1)+timeNew.getDay();
                 var timeS=dataOfPort.reserve.ResDate.split('-').join("");*/
                var options = {};
                options.tableClassId = dataOfPort.reserve.tableClassId;
                options.successFunction = function (dataT) {
                    fun();

                    $('#subCancelTaskPage').html('');
                    $('<div class="cont">\
                    <div class="main">\
                    <dl class="menuChose">\
                        <div>预定方式</div>\
                        <p>微信预定</p>\
                    </dl>\
                    <dl class="personInfo">\
                    <div class="listHead">联系方式</div>\
                    <pre>姓名：'+dataOfPort.reserve.name+'\
                    <br>电话：'+dataOfPort.reserve.ResMobile+'\
                </pre>\
                <a href="tel:'+dataOfPort.reserve.ResMobile+'"></a>\
                </dl>\
                <dl class="severCont">\
                    <div>服务内容</div>\
                <pre>预定餐位\
                <br>\
用餐时间：'+time+dataOfPort.reserve.ResTime+'\
<br>\
用餐人数：'+dataT.data[0].cap+'人\
                </pre>\
                </dl>\
                <dl class="menuChose">\
                    <div>账单情况</div>\
                    <p>未点餐</p>\
                    </dl>\
                        <dl class="menuChose">\
                            <div>支付状态</div>\
                            <p>未支付</p>\
                            <h6>〉</h6>\
                        </dl>\
                        <dl class="refuseReason">\
                            <div>拒绝理由</div>\
                            <textarea cols="40" rows="4" class="refuseReserveReson"></textarea>\
                        </dl>\
                    </div>\
                    </div>').appendTo('#subCancelTaskPage');

                    $(' <div class="bottom" >\
                    <div data-function="btn_refuseReasonOk" data-id="'+dataOfPort.id+'">确认拒绝任务</div>\
                    </div>').appendTo('#subCancelTaskPage');
                };
                OrderingSystem.getTableClassByTableClassId(options);
            }else{
                $('.modalOrderIsAccepted').show();
                $('.wait_gear').hide();
            }



        });
    }
    /*
     * 拒绝预定填写理由
     * */
    var refuseReserveReason = function(obj,fun){
        var options = {};
        options.orderId =obj.data('id');
        options.rejectReason = $(".refuseReserveReson").val();
        options.successFunction = function (data) {
            if(data.error>0){
                if(data.error =="33095"){
                    $('.modalOrderIsAccepted').show();
                    $('.wait_gear').hide();
                }else{
                    alert("请填写拒绝理由")
                    $('.wait_gear').hide();
                }

            }else{
                reserveListFun(fun);
            }


        };
        OrderingSystem.rejectOrder(options);
    }
    /*
     * 接受预定
     * */
    var acceptReserve = function(obj,fun){
        var options = {};
        options.OrderId=obj.data('id');
        options.successFunction = function (data) {
            if(data.error>0){
                $('.modalOrderIsAccepted').show();
                $('.wait_gear').hide();
            }else{
                var options = {};
                options.orderId = obj.data('id');
                options.successFunction = function (data) {
                    $('.chooseSeatIndexPage').show();
                    $('.chooseSeatIndexMain').show();
                    $('.yesLuoZhuo').data('id',options.orderId);
                    $('.wait_gear').hide();
                    if(fun){
                        fun();
                    }
                };
                OrderingSystem.aceeptOrder(options);
            }

        };
        OrderingSystem.catchOrderByUid(options);

    }
    /*
     *落桌yes
     * */
    var luozhuoShowList = function(obj,fun){
        LHUOZHUO.orderId=obj.data('id');
        OrderingSystem.getTableList(function (data) {
            reserveListFun(fun);
            $('.chooseSeatIndexPage').hide();
            $('.chooseSeatIndexMain').hide();
            var capital = ['','壹','贰','叁','肆','伍','陆','柒','捌','玖','拾']
            $('.ul-table-list').html('');
            $('<div class="listTitle">\
                <h2>餐台列表</h2>\
                </div>').appendTo('.ul-table-list');
            $.each(data.data,function(k,v){
                if(v.tableStatus=="OPEN"){
                    var number=(v.tableCap<11)?capital[v.tableCap]:"拾";
                    $('<li data-function="btn_LetReserveChooseTable" data-id="'+v.id+'">\
                   <div data-function="btn_LetReserveChooseTable" data-id="'+v.id+'">'+number+'</div>\
                   <p data-function="btn_LetReserveChooseTable" data-id="'+v.id+'">'+v.label+'（空） '+v.tableClassName+'</p>\
                   </li>').appendTo('.ul-table-list')
                }

            })
        });
    }
    /*
     *落桌no
     * */
    var luoZhuoNo = function(fun){
        reserveListFun(fun);
    }
    /*
     * 落桌选坐
     * */
    var luoZhuoSelecTable = function(obj,fun){

        var options = {};
        options.orderId = LHUOZHUO.orderId;
        options.tableId = obj.data('id');
        options.successFunction = function (data) {
            reserveListFun(fun);
            LHUOZHUO.orderId=null;
        };
        OrderingSystem.pushReserveOrderToTable(options);
    }

    /*
     * 点击预定详情里向右的箭头
     * */
    var clickReserveInfoRight = function (obj,fun) {
        var oId = obj.data('id');
        OrderingSystem.getOrderById(oId, function (data) {
            $('.rightArrowsIconInfo').html('');
            var dataOfPort = data.data[0];
            var cpList = "";
            var request="";
            var token="";
            if(dataOfPort.request.length==0){

            }else{
                request= dataOfPort.specialRequest;
            }
            if(dataOfPort.tokenList[0]){
                token=dataOfPort.tokenList[0].name;
            }else{
                token='无'
            }
            $.each(dataOfPort.itemList, function (k, v) {
                cpList += '<dl class="menuList">\
                    <p class="dishesName">' + v.name + '</p>\
                    <p class="price">￥' + v.price / 100 + '</p>\
                    <p class="count">*' + v.count + '</p>\
                    </dl>'

            })
            var orderId = oId;
            var tokenId=[];
            $.each(dataOfPort.tokenList,function(k,v){
                tokenId.push(v.id);
            })

            OrderingSystem.caluAmount(orderId, tokenId, function (data) {
                fun();
                var discountMoney=data.data[0].caluAmount?(data.data[0].caluAmount/100).toFixed(2):"0.00";
                $('<div class="main">\
                <dl class="demand">\
                <p class="demandInfo">特殊需求：</p>\
                <div class="demandDetail">'+request+'</div>\
                </dl>\
                '+cpList+'\
                    <dl class="menuList">\
                        <p class="price">合计：'+dataOfPort.total/100+'元</p>\
                    </dl>\
                    <dl class="menuList">\
                        <p class="numSum">商品数量</p>\
                        <p class="price">优惠券：'+token+'</p>\
                    </dl>\
                    <dl class="menuList">\
                        <p class="numSum">共计：'+dataOfPort.itemList.length+'件</p>\
                        <p class="price">应收：'+discountMoney+'元</p>\
                    </dl>\
                </div>').appendTo('.rightArrowsIconInfo');
                cpList="";
            });


        });
    };
    /*
     * 添加预定
     * */
    var addReserve = function(fun,addCpfun){
        var date=new Date();
        date=date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate();
        var options = {};
        options.name = $(".createPreOrder_name").val();
        options.sex = '1';
        options.ResMobile = $(".createPreOrder_ResMobile").val();
        options.ResDate = date;
        options.ResTime = $.trim($(".createPreOrder_ResTime").html());
        options.tableClassId = $.trim($('#subaddOrderPage .countText').data('id'));
        options.successFunction = function (data) {
            if(data.error>0){
                alert('请补全信息')
                $(".wait_gear").fadeOut(100);
            }else{
                //showPage($('#suborderMainPage'));
                reserveListFun(fun);
                addCpfun(data.data[0].id);//添加菜品
                $(".createPreOrder_name").val("");
                $(".createPreOrder_ResMobile").val("");
                $(".countText").val("");
                if(LHUOZHUO.tableId){
                    var options = {};
                    options.orderId = data.data[0].id;
                    options.tableId = LHUOZHUO.tableId;

                    options.successFunction = function (data) {

                        LHUOZHUO.tableId=null;
                    };
                    OrderingSystem.pushReserveOrderToTable(options);
                }else{

                }
                var optionsS= {};
                optionsS.orderId = data.data[0].id;
                optionsS.commonRequest = [];
                optionsS.specialRequest = $.trim($('#submenuPage .specialDemand').val());

                optionsS.successFunction = function (data) {

                };
                OrderingSystem.setOrderRequest(optionsS);//设置口味
            }


        };
        OrderingSystem.createPreOrder(options);
    }
    /*
     * 添加预定的选择桌号
     * */
    var addReserveSelectTable = function(fun){
        OrderingSystem.getTableList(function (data) {
            fun();
            $('.chooseSeatIndexPage').hide();
            $('.chooseSeatIndexMain').hide();
            var capital = ['','壹','贰','叁','肆','伍','陆','柒','捌','玖','拾']
            $('.ul-table-list').html('');
            $('<div class="listTitle">\
                <h2>餐台列表</h2>\
                </div>').appendTo('.ul-table-list')
            $.each(data.data,function(k,v){
                if(v.tableStatus=="OPEN"){
                    var number=(v.tableCap<11)?capital[v.tableCap]:"拾";
                    $('<li data-function="btn_tableList_addReserveTable" data-id="'+v.id+'">\
                   <div data-function="btn_tableList_addReserveTable" data-id="'+v.id+'">'+number+'</div>\
                   <p data-function="btn_tableList_addReserveTable" data-id="'+v.id+'">'+v.label+'（空） '+v.tableClassName+'</p>\
                   </li>').appendTo('.ul-table-list');
                }

            })
        });
    }
    /*
     * 桌台列表页面，添加桌台时直接选择桌台
     * */
    var directChooseTableOfTableList = function(obj,fun){
        LHUOZHUO.tableId=obj.data('id');
        fun();
    }
    /*
     * 添加按钮到预定界面
     * */
    var addBtnToAddReservePage = function(fun){
        $('#subaddOrderPage .contDishes .dishes').html("菜品：")
        $('#subaddOrderPage .demand').html("特殊需求：")

        var options = {};
        options.successFunction = function (data) {
            $('#subaddOrderPage .tableTypePage').html('');
            fun();
            $.each(data.data,function(k,v){
                $('<li class="tableType" data-function="btn_addReserveTableType" data-id="'+v.id+'">'+v.name+'</li>').appendTo('#subaddOrderPage .tableTypePage');
            })
        };
        OrderingSystem.getTableClassList(options);

    }
    return{
        reserveListFun:function(fun){
            reserveListFun(fun);
        },
        clickReserveNotAcceptedONReserveList:function(obj,fun){
            clickReserveNotAcceptedONReserveList(obj,fun);
        },
        refuseReserve:function(obj,fun){
            refuseReserve(obj,fun);
        },
        refuseReserveReason:function(obj,fun){
            refuseReserveReason(obj,fun);
        },
        acceptReserve:function(obj,fun){
            acceptReserve(obj,fun);
        },
        luozhuoShowList:function(obj,fun){
            luozhuoShowList(obj,fun);
        },
        luoZhuoNo:function(fun){
            luoZhuoNo(fun);
        },
        acceptedReserveInfo:function(obj,fun){
            acceptedReserveInfo(obj,fun);
        },
        clickReserveInfoRight:function(obj,fun){
            clickReserveInfoRight(obj,fun);
        },
        //添加预定
        addReserve:function(fun,addCpfun){
            addReserve(fun,addCpfun);
        },
        luoZhuoSelecTable:function(obj,fun){
            luoZhuoSelecTable(obj,fun)
        },
        //添加预定时直接选择桌台
        addReserveSelectTable:function(fun){
            addReserveSelectTable(fun);
        },
        //桌台列表页面，添加桌台时直接选择桌台
        directChooseTableOfTableList:function(obj,fun){
            directChooseTableOfTableList(obj,fun);
        },
        //添加按钮到预定界面
        addBtnToAddReservePage:function(fun){
            addBtnToAddReservePage(fun);
        }


    }
}();